//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_docker_template_api_serv

import (
	"context"

	"atomgit.com/openlinksaas/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/docker_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/docker_template_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *DockerTemplateAdminImpl) CreateCate(ctx context.Context, req *docker_template_api.AdminCreateCateRequest) (*docker_template_api.AdminCreateCateResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminCreateCateResponse{
			Code:   docker_template_api.AdminCreateCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminCreateCateResponse{
			Code:   docker_template_api.AdminCreateCateResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.CreateCate {
		return &docker_template_api.AdminCreateCateResponse{
			Code:   docker_template_api.AdminCreateCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	cateId := uuid.NewString()
	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.CateDao.Insert(sessCtx, &docker_dao.CateInfoDbItem{
			CateId:   cateId,
			CateName: req.CateName,
			AppCount: 0,
		})
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminCreateCateResponse{
			Code:   docker_template_api.AdminCreateCateResponse_CODE_OK,
			CateId: cateId,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminCreateCateResponse), nil
}

func (apiImpl *DockerTemplateAdminImpl) UpdateCate(ctx context.Context, req *docker_template_api.AdminUpdateCateRequest) (*docker_template_api.AdminUpdateCateResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminUpdateCateResponse{
			Code:   docker_template_api.AdminUpdateCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminUpdateCateResponse{
			Code:   docker_template_api.AdminUpdateCateResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.UpdateCate {
		return &docker_template_api.AdminUpdateCateResponse{
			Code:   docker_template_api.AdminUpdateCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	exist, err := docker_dao.CateDao.Exist(ctx, req.CateId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminUpdateCateResponse{
			Code:   docker_template_api.AdminUpdateCateResponse_CODE_NO_CATE,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.CateDao.Update(sessCtx, req.CateId, req.CateName)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminUpdateCateResponse{
			Code: docker_template_api.AdminUpdateCateResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminUpdateCateResponse), nil
}

func (apiImpl *DockerTemplateAdminImpl) RemoveCate(ctx context.Context, req *docker_template_api.AdminRemoveCateRequest) (*docker_template_api.AdminRemoveCateResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminRemoveCateResponse{
			Code:   docker_template_api.AdminRemoveCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminRemoveCateResponse{
			Code:   docker_template_api.AdminRemoveCateResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.RemoveCate {
		return &docker_template_api.AdminRemoveCateResponse{
			Code:   docker_template_api.AdminRemoveCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	cateItem, err := docker_dao.CateDao.Get(ctx, req.CateId)
	if err != nil {
		return &docker_template_api.AdminRemoveCateResponse{
			Code:   docker_template_api.AdminRemoveCateResponse_CODE_NO_CATE,
			ErrMsg: "分类不存在",
		}, nil
	}
	if cateItem.AppCount > 0 {
		return &docker_template_api.AdminRemoveCateResponse{
			Code:   docker_template_api.AdminRemoveCateResponse_CODE_HAS_APP,
			ErrMsg: "还有应用未删除",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.CateDao.Remove(sessCtx, req.CateId)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminRemoveCateResponse{
			Code: docker_template_api.AdminRemoveCateResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminRemoveCateResponse), nil
}
